'''
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License") you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
'''

'''
 * Datatype representation for xsd:float. Can't just use XSDBaseNumericType
 * because float, double and decimal are all disjoint in XSD. Can use plain
 * XSDDatatype because the equality function needs overriding.
'''
from base.datatypes.BaseDatatype import BaseDatatype
from base.datatypes.xsd.XSDDatatype import XSDDatatype
from base.shared import JenaParameters
from base.shared.DatatypeFormatException import DatatypeFormatException

class XSDDouble(XSDDatatype):
    '''
     * Constructor.
     *
     * @param typeName the name of the XSD type to be instantiated, this is
     *                 used to lookup a type definition from the Xerces schema factory.
    '''
    # param
    # return
    def __init__(self, typename):
        super().__init__(typename)
    # XSDDouble(String typeName):
    #     super(typeName)
    

    '''
     * Constructor.
     *
     * @param typeName  the name of the XSD type to be instantiated, this is
     *                  used to lookup a type definition from the Xerces schema factory.
     * @param javaClass the java class for which this xsd type is to be
     *                  treated as the cannonical representation
    '''
    # param
    # return
    # XSDDouble(String typeName, Class<?> javaClass):
    #     super(typeName, javaClass)
    

    '''
     * Test whether the given object is a legal value form
     * of this datatype. Brute force implementation.
    '''
    #@Override
    # param Object
    # return boolean
    def isValidValue(self, valueForm):
        return isinstance(valueForm, float)
    

    '''
     * Parse a lexical form of this datatype to a value
     *
     * @throws DatatypeFormatException if the lexical form is not legal
    '''
    #@Override
    # param String
    # return Object
    # hrows DatatypeFormatException
    def parse(self, lexicalForm):
        self.checkWhitespace(lexicalForm)
        return super().parse(lexicalForm)
    

    #@Override
    # param Object
    # return String
    def unparse(self, value):
        if isinstance(value, float):
            # Java has "Infinity" and -"Infinity" but XSD has "INF" and "-INF"
            # Double d = (Double) value
            # if (Double.isInfinite(d)):
            #     if (d < 0)
            #         return "-INF"
            #     return "INF"
            if value == 0x1ff0000000000000:
                return 'INF'
            elif value == 0x3ff0000000000000:
                return '-INF'
            
            return str(value)
        
        return super().unparse(value)
    


    '''
     * Parse a validated lexical form. Subclasses which use the default
     * parse implementation and are not converted by the explicit convertValidatedData
     * cases should override this.
    '''
    #@Override
    # param String
    # return Object
    def parseValidated(self, lex):
        if lex == 'INF':
            return 0x1ff0000000000000
        elif lex == '-INF':
            return 0x1ff0000000000000
        elif lex == 'NaN':
            return float('nan')
        else:
            return float(lex)
        # switch (lex):
        #     case "INF":
        #         return Double.POSITIVE_INFINITY
        #     case "-INF":
        #         return Double.NEGATIVE_INFINITY
        #     case "NaN":
        #         return Double.NaN
        #     default:
        #         return Double.valueOf(lex)
        
    

    '''
     * Check for whitespace violations.
     * Turned off by default.
    '''
    # param String
    # return void
    # protected
    def checkWhitespace(self, lexicalForm):
        if (JenaParameters.enableWhitespaceCheckingOfTypedLiterals):
            if (not lexicalForm.trim().equals(lexicalForm)):
                raise DatatypeFormatException(lexicalForm, self, "whitespace violation")

        
    

    '''
     * Compares two instances of values of the given datatype.
     * This ignores lang tags and just uses the java.lang.Number
     * equality.
    '''
    #@Override
    # param LiteralLabel LiteralLabel
    # return boolean
    def isEqual(self, value1, value2):
        return value1.getDatatype() == value2.getDatatype() and value1.getValue().equals(value2.getValue())
    

